package orfeu.engine;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;

public class DatabaseHelper extends SQLiteOpenHelper {

	
	static final String DB_NAME="orfeu";
	private static String DB_PATH = "/data/data/orfeu.engine/databases/";
	static final String highscoresTable="highscores";

	private  Context myContext;
    public SQLiteDatabase myDataBase; 

	
	public DatabaseHelper(Context context) {
		super(context, DB_NAME, null,33);
		 this.myContext = context;
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		
		
		
	}

	public void createDataBase() throws IOException{
   	 
    	boolean dbExist = checkDataBase();
    	System.out.println(dbExist+"");
    	
    	try{
    //	openDataBase();
    	String sql = "DROP TABLE "+highscoresTable;
    //	myDataBase.execSQL(sql);
    	
    	sql = "CREATE TABLE "+highscoresTable +"(ID INTEGER PRIMARY KEY AUTOINCREMENT, User STRING default 'user', Highscore INTEGER default 0)";
    	myDataBase.execSQL(sql);
    	}catch(Exception e)
    	{
    		
    	}

    	if(dbExist){
    		System.out.println("Hello2");
    		// baza de date exista.
    		//Toast toast=Toast.makeText(DatabaseHelper.this, "Exista", Toast.LENGTH_LONG);
    		//toast.show();
    	}else{
    		// Creaza noua baza de date in folderul default
        	this.getReadableDatabase();
        	try {
    			copyDataBase();
    		} catch (IOException e) {
 
        		throw new Error("Error copying database");
 
        	}
    	}
 
    }
	  private void copyDataBase() throws IOException{
		  
	    	InputStream myInput = myContext.getAssets().open(DB_NAME);
	    	String outFileName = DB_PATH + DB_NAME;
	    	OutputStream myOutput = new FileOutputStream(outFileName);
	 
	    	byte[] buffer = new byte[1024];
	    	int length;
	    	while ((length = myInput.read(buffer))>0){
	    		myOutput.write(buffer, 0, length);
	    	}
	 
	    	myOutput.flush();
	    	myOutput.close();
	    	myInput.close();
	 
	    }
	private boolean checkDataBase(){
		 
    	SQLiteDatabase checkDB = null;
 
    	try{
    		String myPath = DB_PATH + DB_NAME;
    		checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
 
    	}catch(SQLiteException e){ }
 
    	if(checkDB != null)
    		checkDB.close();
 
    	return checkDB != null ? true : false;
    }

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		
	}
	public void openDataBase() throws SQLException{
		 
        String myPath = DB_PATH + DB_NAME;
    	myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
    }
	 @Override
	public synchronized void close() {
	 
	    	    if(myDataBase != null)
	    		    myDataBase.close();
	 
	    	    super.close();
	}
	 public void insertHighscore(String user, int score){
			
			myDataBase.execSQL("INSERT INTO " + highscoresTable +
					" VALUES ('"+score+"'"+ " ," + "null"+","+"'"+user+"');");
			
		}
	
}

